﻿using Microscan.SDK;
using MSDK = Microscan.SDK;

namespace NDK.Module.Model
{
    public class MscBarcoddata
    {
        string ipAddress;
        public string Barcod=string.Empty;
        MSDK.ReaderDevice _reader;
        MSDK.ReaderCycleReportConnection _reportConnection;
        MSDK.ImageOptions _imageOptions;
        //readonly MSDK.DeviceDiscovery _discovery = MSDK.Connector.Instance.Discovery;
        public MscBarcoddata(string IpAddress)
        {
            ipAddress = IpAddress;
        }


        public void UseReader()
        {
            // get the Connector
            var connector = Connector.Instance;
            _reportConnection = null;
            _reader = null;

            // use a reader device
            _reader = connector.UseReader(ipAddress);

            // configure the report connection
            _reportConnection = _reader.ReportConnection;
            _reportConnection.Throttle = 300; //in ms - prevent reports from coming in too fast

            // set any image options
            _imageOptions = _reportConnection.ImageOptions;
            _reportConnection.NewReport += ReportConnection_NewReport;
            _imageOptions.ImageFetch = IMAGE_FETCH.FIRST_QUALIFIED;
            _imageOptions.ImageFormat = IMAGE_FORMAT.PNG;
            _imageOptions.Decimate = 0;
            _imageOptions.DrawGraphics = true;

        }

        public void toggleStart()
        {
            if (_reportConnection.IsOpen())
            {
                _reportConnection.Stop();
            }
            else
            {
                if (_reader.IpAddr != ipAddress)
                {
                    UseReader();
                }
                _reportConnection.Start();
            }

        }

        public void ReportConnection_NewReport(MSDK.ReaderCycleReport report)
        {
            //if (Barcod != report.outData && report.outData != "" )
            {
                Barcod = report.outData;
            }
        }
    }
}
